using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace RaisingStudio.Data.Common
{
    public class DataContextBase : DataTransactionBase
    {
        #region Constructor
        /// <summary>
        /// Constructor
        /// </summary>
        public DataContextBase()
        {
            this._logManager = new LogManager();
        }

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="connection">database connection.</param>
        public DataContextBase(System.Data.IDbConnection connection)
            : base(connection)
        {
            this._logManager = new LogManager();
        }

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="transactionManager">database transaction manager.</param>
        public DataContextBase(TransactionManager transactionManager)
            : base(transactionManager)
        {
            this._logManager = new LogManager();
        }

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="connectionName">the name of connection.</param>
        public DataContextBase(string connectionName)
            : base(connectionName)
        {
            this._logManager = new LogManager();
        }

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="databaseType">database type.</param>
        /// <param name="connectionString">connection string.</param>
        public DataContextBase(DatabaseType databaseType, string connectionString)
            : base(databaseType, connectionString)
        {
            this._logManager = new LogManager();
        }

        public DataContextBase(string providerName, string connectionString)
            : base(providerName, connectionString)
        {
            this._logManager = new LogManager();
        }


        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="transactionManager">database transaction manager.</param>
        public DataContextBase(TransactionManager transactionManager, LogManager logManager)
            : base(transactionManager)
        {
            this._logManager = logManager;
        }

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="transactionManager">database transaction manager.</param>
        public DataContextBase(TransactionManager transactionManager, LogManager logManager, DatabaseType databaseType, string providerName)
            : base(transactionManager)
        {
            this._logManager = logManager;
            base.DatabaseType = databaseType;
            base.ProviderName = providerName;
        }
        #endregion

        private LogManager _logManager;

        public ILogManager LogManager
        {
            get { return _logManager; }
        }
        
        public TextWriter Log
        {
            get
            {
                return this.LogManager.Log;
            }
            set
            {
                this.LogManager.Log = value;
            }
        }

        public virtual void WriteLine(string value)
        {
            if (this.LogManager != null)
            {
                this.LogManager.WriteLine(value);
            }
        }

        public virtual void WriteLine(string format, object arg0)
        {
            if (this.LogManager != null)
            {
                this.LogManager.WriteLine(format, arg0);
            }
        }

        public virtual void WriteLine(string format, params object[] args)
        {
            if (this.LogManager != null)
            {
                this.LogManager.WriteLine(format, args);
            }
        }

        public virtual void WriteLog(System.Data.IDbCommand command)
        {
            bool writeLog = (this._logManager != null && this._logManager.Log != null);
            if (writeLog)
            {
                string commandText = command.CommandText;
                WriteLine(commandText);
                foreach (System.Data.IDbDataParameter parameter in command.Parameters)
                {
                    // sample, -- @p0: Input NVarChar (Size = 6; Prec = 0; Scale = 0) [London]
                    WriteLine("-- {0}: {1} {2} (Size = {3}; Prec = {4}, Scale = {5}) [{6}]", parameter.ParameterName, parameter.Direction, parameter.DbType, parameter.Size, parameter.Precision, parameter.Scale, parameter.Value);
                }
                //sample, -- Context: OTHER, oracle.10.1
                WriteLine("-- Context: {0}, {1}", this.DatabaseType, this.ProviderName);
                WriteLine(string.Empty);
            }
        }
    }
}
